package com.janyee.buddha.util

import android.graphics.Paint
import android.graphics.Rect
import android.util.Log
import android.util.TypedValue

import com.janyee.buddha.entity.LineText
import com.janyee.buddha.entity.PageEntity
import com.janyee.buddha.view.Painter
import com.janyee.buddha.view.TextPainter
import java.util.regex.Pattern

/**
 * Created by kmlixh on 17/11/27.
 * useage:
 * TextTest
 */

object ReaderUtil {
    fun generateLineTextFromIndex(paint: Painter, maxWidth: Float, maxHeight: Float, lineSpacing: Float, builder: StringBuilder, index: Int): PageEntity {
        var lineHeight = paint.getLineHeight()
        var rows:Int= (maxHeight/(lineHeight+lineSpacing)).toInt()
        var list=ArrayList<LineText>()
        var counts=0
        var idx=index
        while (rows>0&&idx<builder.length-1){
            var pair=getLineTextFromIndex(paint,maxWidth,builder,idx)
            list.add(pair)
            counts += pair.length()
            idx += pair.length()
            rows--
        }
        return PageEntity(list, index, counts)
    }

    fun generateLineTextToIndex(paint: Painter, maxWidth: Float, maxHeight: Float, lineSpacing: Float, builder: StringBuilder, index: Int): PageEntity {
        var lineHeight = paint.getLineHeight()
        var rows:Int= (maxHeight/(lineHeight+lineSpacing)).toInt()
        var counts=rows*(maxWidth/paint.getSingleWordWidth()).toInt()
        var rawIdx=0
        if(counts<=index){
            rawIdx= (index-counts)
        }
        if (rawIdx < 0) {
            rawIdx = 0
        }
        if (rawIdx == index && index == 0) {
            return PageEntity()
        }
        var loop=true
        var pair: PageEntity? = null
        while(loop){
            pair= generateLineTextFromIndex(paint,maxWidth,maxHeight,lineSpacing,builder,rawIdx)
            if (pair.count + rawIdx >= index) {
                break
            } else {
                rawIdx ++
            }
        }
        return pair!!
    }
    private fun getLineTextFromIndex(paint: Painter, maxWidth: Float, builder: StringBuilder, index: Int): LineText {
        val count = getCountFromIndex(paint.getNormalTextPaint(), maxWidth, builder, index)
        val lineText = LineText(index, count, -1, -1)
        return lineText
    }


    fun getCountFromIndex(paint: Paint, maxWidth: Float, builder: StringBuilder, index: Int): Int {
        val singleWidth = paint.measureText("测")
        var counts = (maxWidth / singleWidth).toInt()
        if (counts == 0) {
            return 0
        }
        if (builder.length <= index + counts) {
            counts = builder.length - index - 1
        }
        val text=builder.substring(index,index+counts)
        if(text.contains("\n")&&text.indexOf("\n")<builder.length){
            counts=text.indexOf("\n")+1
        }else if (index + counts < builder.length - 1) {
            var pattern = Pattern.compile("\\,|\\.|!|！|\\?|？|，|。|”|’|》")
            var matcher = pattern.matcher(builder.subSequence(index + counts, index + counts + 1))
            if (matcher.find()) {
                counts--
            }
        }
        return counts
    }
}
